package cn.thr.pethome.shop.service.impl;

import cn.thr.pethome.constants.BaseConstants;
import cn.thr.pethome.shop.domain.Shop;
import cn.thr.pethome.shop.mapper.ShopMapper;
import cn.thr.pethome.shop.query.ShopQuery;
import cn.thr.pethome.shop.service.IShopService;
import cn.thr.pethome.base.service.Impl.BaseServiceImpl;
import cn.thr.pethome.util.AuthService;
import cn.thr.pethome.util.BusinessException;
import cn.thr.pethome.user.domain.Employee;
import cn.thr.pethome.user.mapper.EmployeeMapper;
import cn.thr.pethome.util.MD5Utils;
import cn.thr.pethome.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author thr
 * @since 2023-01-07
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private AuthService authService;

    @Override
    public void add(Shop shop) {
        //注册数据不能为空

        if (shop.getAddress()==null){
            throw new BusinessException("地址是空的");
        }
        if (shop.getTel()==null){
            throw new BusinessException("电话是是空的");
        }


        //百度Ai智能审核店名
        if(!authService.authentication(shop.getName())){
            throw new BusinessException("你的店铺名有问题");
        }

        //注册重复验证
        ShopQuery shopQuery = new ShopQuery();
        shopQuery.setName(shop.getName());
        Long count = shopMapper.findByQueryCount(shopQuery);
        //判断店铺名重复没有
        if(count!=0){
            throw new BusinessException("该["+shop.getName()+"]已经存在");
        }

        //补充默认数据
        shop.setRegisterTime(new Date());//设置创建时间
        shop.setState(BaseConstants.Shop.STATE_WAIT_AUTID);//审核状态


        //获取到当前注册的管理员信息
        Employee admin = shop.getAdmin();
        //生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        admin.setSalt(salt);
        String saltPassword = MD5Utils.encrypByMd5(admin.getPassword() + salt); //这是管理
        admin.setPassword(saltPassword);
        //保存数据
        employeeMapper.add(admin);//当我保存  那么员工的id就有了

        shop.setAdminId(admin.getId()); //将员工的id设置为管理员
        shopMapper.add(shop); //当我保存员工的id就有了

        admin.setShopId(shop.getId()); //将店铺的id 设置给员工
        employeeMapper.update(admin); //将数据保存进去
    }

    @Override
    public void update(Shop shop) {

    }
    @Override
    public void activation(Long id) {
        Employee employee = employeeMapper.findOne(id);
        if(employee != null){
            employee.setState(BaseConstants.Employee.STATE_NORMAL);
            employeeMapper.update(employee);
            Shop shop = shopMapper.findOne(employee.getShopId());
            shop.setState(BaseConstants.Shop.STATE_ACTIVE_SUCCESS);
            shopMapper.update(shop);
        }else {
            throw new BusinessException("没有在查到你的信息");
        }
    }

}
